<?php
namespace Common\Logic\Admin;

use Common\Base\LogicBase;
use Common\Model\Admin\NodeModel;

class NodeLogic extends LogicBase
{
	public $m_node;
    public $menu_level = array(
        1=>'一级菜单',
        2=>'二级菜单',
        3=>'三级菜单',
        4=>'其他菜单'
    );

	public function __construct()
	{
		$this->m_node = new NodeModel();
	}

    /**
     * 得到菜单等级select
     *
     * @param int     $choose 默认选中
     * @param string  $name 名称、ID
     * @param string  $onchange 事件
     * @param boolean $disabled
     *
     * @return string
     */
    public function getMenuLevelSelect($choose=-1, $name='node_level', $onchange='', $disabled=false)
    {
        $dis = $disabled ? 'disabled="disabled"' : '';
        $html = '<select name="'.$name.'" id="'.$name.'" onchange="'.$onchange.'" '.$dis.'><option value="-1">--请选择--</option>';
        $choose = intval($choose);
        foreach($this->menu_level as $k=>$v){
            $sel = ($k === $choose) ? 'selected="selected"' : '';
            $html .= '<option value="'.$k.'" '.$sel.'>'.$v.'</option>';
        }
        $html .= '</select>';
        return $html;
    }

	/**
     * 得到节点列表
     *
     * @param int $w
     * @param array $search
     * @param string $fields
     * @param int $page
     * @param int $pageSize
     * @param string $by
     * @param string $exclude
     *
     * @return array
     */
    public function getNodeList($w=3, $search=null, $fields='*', $page=null, $pageSize=null, $by='', $exclude=null)
    {
        return $this->m_node->getNodeList($w, $search, $fields, $page, $pageSize, $by, $exclude);
    }

    /**
     * 添加节点
     *
     * @param array $datas
     *
     * @return array
     */
    public function addNode($datas)
    {
        if(isset($datas['node_name']) && $datas['node_name']!=''){
            $node_name = $datas['node_name'];
        }else{
            $this->setError('请输入节点名称');
            return false;
        }
        if(isset($datas['m_c_a']) && $datas['m_c_a']!=''){
            $m_c_a = $datas['m_c_a'];
        }else{
            $this->setError('请输入节点地址');
            return false;
        }
        if(isset($datas['node_pid'])){
            $node_pid = abs(intval($datas['node_pid']));
        }else{
            $node_pid = 0;
        }
        if(isset($datas['node_level'])){
            if(in_array(intval($datas['node_level']), array(1, 2, 3, 4))){
                $node_level = intval($datas['node_level']);
            }else{
                $node_level = 4;
            }
        }else{
            $node_level = 4;
        }
        if(isset($datas['px'])){
            $px = intval($datas['px']);
            if($px < 1){ $px = 1; }
        }else{
            $px = 1;
        }
        //得到节点分级路径
        if($node_pid == 0){
            $node_pid_path = ',0,';
        }else{
            $tmp = $this->getNodeList(4, array('node_id'=>$node_pid, 'node_pid_path'));
            $node_pid_path = $tmp['node_pid_path'].$node_pid.',';
        }

        $row = array();
        $row['node_pid'] = $node_pid;
        $row['node_pid_path'] = $node_pid_path;
        $row['m_c_a'] = $m_c_a;
        $row['node_name'] = $node_name;
        $row['node_level'] = $node_level;
        $row['px'] = $px;
        if(isset($datas['css']) && !empty($datas['css'])){
        	$row['icon'] = $datas['css'];
        }
        
        $insertID = $this->m_node->addNode($row);
        if(false === $insertID){
            $this->setError($this->m_node->getError());
            return false;
        }else{
            return $insertID;
        }
    }

     /**
     * 删除节点
     *
     * @param int $node_id
     *
     * @return array
     */
    public function delNode($node_id = 0)
    {
        $node_id = intval($node_id);
        if($node_id < 1){ $this->setError('参数错误'); return false; }
        //检查有没有子菜单
        $ck = $this->getNodeList(1, array('node_pid_path'=>','.$node_id.','));
        if($ck > 0){ $this->setError('有子菜单，不能删除'); return false; }
        $aff = $this->m_node->delNode($node_id);
        if(false === $aff){
            $this->setError($this->m_node->getError());
            return false;
        }else{
            return $aff;
        }
    }

    /**
     * 更新节点
     *
     * @param array $dat 需要更新的数据
     * @param int   $node_id
     *
     * @return array
     */
    public function updateNodeByNodeId($dat=array(), $node_id=0)
    {
        $aff = $this->m_node->updateNodeByNodeId($dat, $node_id);
        if(false === $aff){
            $this->setError($this->m_node->getError());
            return false;
        }else{
            return $aff;
        }
    }

    /**
     * 编辑节点
     *
     * @param array $datas
     *
     * @return array
     */
    public function editNode($datas  = array())
    {
        if(isset($datas['node_id']) && intval($datas['node_id'])>0){
            $node_id = intval($datas['node_id']);
        }else{
            $this->setError('参数错误');
            return false;
        }
        if(isset($datas['node_name']) && $datas['node_name']!=''){
            $node_name = $datas['node_name'];
        }else{
            $this->setError('请输入节点名称');
            return false;
        }
        if(isset($datas['m_c_a']) && $datas['m_c_a']!=''){
            $m_c_a = $datas['m_c_a'];
        }else{
            $this->setError('请输入节点地址');
            return false;
        }
        if(isset($datas['node_level'])){
            if(in_array(intval($datas['node_level']), array(1, 2, 3, 4))){
                $node_level = intval($datas['node_level']);
            }else{
                $node_level = 4;
            }
        }else{
            $node_level = 4;
        }
        if(isset($datas['px'])){
            $px = intval($datas['px']);
            if($px < 1){ $px = 1; }
        }else{
            $px = 1;
        }

        $row = array();
        $row['m_c_a'] = $m_c_a;
        $row['node_name'] = $node_name;
        $row['node_level'] = $node_level;
        $row['px'] = $px;
        if(isset($datas['css']) && !empty($datas['css'])){
        	$row['icon'] = $datas['css'];
        }

        $aff = $this->updateNodeByNodeId($row, $node_id);
        if(false === $aff){
            $this->setError($this->m_node->getError());
            return false;
        }else{
            return $aff;
        }
    }

    /**
     * 节点的css样式
     *
     * @return array
     */
    public function getNodeCss()
    {
        $wz = 'css_fonts';
        //读取字体
        $cache = F($wz);
        if(empty($cache))
        {
            $f = fopen($_SERVER['DOCUMENT_ROOT'].'/zui/zui_icon.txt', 'r');
            $cache = array();
            while( ! feof($f))
            {
                $r = fgets($f);
                $r = trim($r);
                $cache[] = $r;
            }
            if( ! empty($cache)){
                F($wz, $cache);
            }
        }
        return $cache;
    }

}